package com.picket.dao;

import java.util.Collection;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.orm.jdo.support.JdoDaoSupport;
import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.picket.domain.Team;
 
@Repository
public class TeamDao extends JdoDaoSupport implements ITeamDao {
 

  
 @Override
 public void create(final List<Team> teams) {
	 for(Team team : teams){
		  getJdoTemplate().makePersistent(team);
		  System.out.println(team.getKey());
	 }

 }
 
 @Override
 public Collection<Team> getAllTeams() {
 
  final PersistenceManager pm = getPersistenceManager();
   
  try {
 
   final Query query = pm.newQuery(Team.class);
   //query.setOrdering("date descending");
    
   @SuppressWarnings("unchecked")
   final Collection<Team> teams =
    (Collection<Team>) query.execute();
    
   return pm.detachCopyAll((Collection<Team>)teams);
  }
   
  finally {
   releasePersistenceManager(pm);
  }
 
 }
 
 
 @Override
 public Team getTeam(long id) {
 
  final PersistenceManager pm = getPersistenceManager();
   
  try {
	  
	  Key k = KeyFactory.createKey(Team.class.getSimpleName(), id);
      return pm.getObjectById(Team.class, k);

  }
   
  finally {
   releasePersistenceManager(pm);
  }
 
 }
}